package com.amkai.aes.webservice.binary.objecthandler;

import java.io.Serializable;

import com.amkai.aes.aescommon.service.binary.job.RemarkJob;
import com.amkai.aes.aescommon.service.result.RemarkJobResult;
import com.amkai.aes.server.registration.RemarkManager;
import com.amkai.aes.webservice.binary.JobObjectHandler;
import com.amkai.common.extensions.hibernate.config.SiteManager;
import com.amkai.common.extensions.hibernate.config.manager.SessionManager;

public class RemarkJobHandler implements JobObjectHandler<RemarkJob, Serializable, RemarkJobResult>{
	
	@Override
	public RemarkJobResult process(RemarkJob job, Serializable object) throws Exception {
		RemarkJobResult result = new RemarkJobResult();
		
		SessionManager sm = SiteManager.Start();
		sm.openSession();
		try{
			// KKORNEL: AES-9800 - Copied to remark manager
			RemarkManager rm = new RemarkManager(sm);
			switch(job.getKind()){
			case COUNT:
				// KKORNEL: AES-11013, AES-18530
				result.setCount(rm.getRemarkCount(job.getPersonId(), job.getDepartmentId(), job.getRemarkType(), job.getPassportId(), 
								job.isAllowedToListOwnRemarks(), job.isAllowedToListRemarksForOthers(), job.isViewRCMcomments()));					
				break;
			case UPLOAD :
				try{
					rm.uploadRemarks(job.getRemarks());
				}catch(Exception exception){
					sm.rollbackTransaction();
					throw new Exception("RemarkJob", exception);
				}
				// We need a download after upload... the break isn't needed
				//break;
			case DOWNLOAD :				
				// KKORNEL: AES-11013, AES-18530
				result.setRemarks(rm.downloadRemarks(job.getPersonId(), job.getDepartmentId(), job.getRemarkType(), job.getPassportId(), 
						job.isAllowedToListOwnRemarks(), job.isAllowedToListRemarksForOthers(), job.isViewRCMcomments()));					
				break;
			}
		}finally{
			sm.closeSession();
		}
		return result;
	}
}
